/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.portlet.bean.impl;

import java.util.Map;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.util.ObjectUtils;

import com.genia.toolbox.web.portlet.bean.Portlet;

/**
 * Base class of any Portlet that mimics the comportment of another porlet
 * identified by its spring name.
 */
public abstract class AbstractLinkedPortlet
    implements Portlet, BeanFactoryAware
{

  /**
   * the Spring BeanFactory.
   */
  private BeanFactory beanFactory;

  /**
   * the linked porlet.
   */
  private transient Portlet subPortlet;



  /**
   * Indicates whether some other object is "equal to" this one.
   * 
   * @param obj
   *          the reference object with which to compare.
   * @return <code>true</code> if this object is the same as the obj argument;
   *         <code>false</code> otherwise.
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals(final Object obj)
  {
    if (obj == null) {
      return false;
    }
    if (obj.getClass() == this.getClass()) {
      final AbstractLinkedPortlet oPortlet = (AbstractLinkedPortlet) obj;
      return ObjectUtils.nullSafeEquals(getLinkedPortletName(), oPortlet.getLinkedPortletName());
    }
    return false;
  }



  /**
   * getter for the attributes property.
   * 
   * @return the attributes
   */
  public Map<String, String> getAttributes()
  {
    return getSubPortlet().getAttributes();
  }



  /**
   * getter for the beanFactory property.
   * 
   * @return the beanFactory
   */
  public BeanFactory getBeanFactory()
  {
    return beanFactory;
  }



  /**
   * <code>LinkedPortlet</code> have no description.
   * 
   * @return <code>null</code>
   */
  public String getDescriptionName()
  {
    return null;
  }



  /**
   * getter for the identifier property.
   * 
   * @return the identifier
   */
  public String getIdentifier()
  {
    return getSubPortlet().getIdentifier();
  }



  /**
   * returns whether this {@link LinkedPortlet} must keep in cash the sub-porlet
   * it uses.
   * 
   * @return whether this {@link LinkedPortlet} must keep in cash the sub-porlet
   *         it uses
   */
  protected boolean doCashSubPortlet()
  {
    return true;
  }



  /**
   * getter for the linkedPorlet property.
   * 
   * @return the linkedPorlet
   */
  public Portlet getSubPortlet()
  {
    if (doCashSubPortlet()) {
      if (subPortlet == null) {
        subPortlet = (Portlet) getBeanFactory().getBean(getLinkedPortletName());
      }
      return subPortlet;
    }
    else {
      return (Portlet) getBeanFactory().getBean(getLinkedPortletName());
    }
  }



  /**
   * returns the spring name of the linked portlet.
   * 
   * @return the spring name of the linked portlet
   */
  public abstract String getLinkedPortletName();



  /**
   * Returns a hash code value for the object. This method is supported for the
   * benefit of hashtables such as those provided by
   * <code>java.util.Hashtable</code>.
   * 
   * @return a hash code value for this object.
   * @see java.lang.Object#hashCode()
   */
  @Override
  public int hashCode()
  {
    return ObjectUtils.nullSafeHashCode(getLinkedPortletName());
  }



  /**
   * setter for the beanFactory property.
   * 
   * @param beanFactory
   *          the beanFactory to set
   */
  public void setBeanFactory(final BeanFactory beanFactory)
  {
    this.beanFactory = beanFactory;
  }

}
